<?php	
namespace app\pay\controller\yop;

use think\Controller;
use pay\yop\Config;

use pay\yop\lib\YopRequest;
use pay\yop\lib\YopClient3;
use customtool\Utils;

class DaifuQuery extends Base {
    private $cfg = null;
    
    public function _initialize() {
        $this->cfg = new Config();
    }
    
    /*
     * @params
     * $data    -> post参数
     * $resPayBis     -> model('PayBis')相关参数
     */
    public function index($resPayBis, $resPayDaifuApply, $resPayPlatform) {
        $res = $this->reqYopData($resPayDaifuApply, $resPayPlatform);
        return $res;
    } 
    
    /*
     * 对数据进行解析，向平台发起请求
     */
    private function reqYopData($resPayDaifuApply, $resPayPlatform) {
        $request = new YopRequest("OPR:" . $resPayPlatform->parent_merchant_id, $resPayPlatform->private_key, $this->cfg->C('yopUrl'), $resPayPlatform->public_key);
        $request->addParam("customerNumber", $resPayPlatform->parent_merchant_id); // 商户编号
        $request->addParam("groupNumber",    $resPayPlatform->group_merchant_id); // 集团商户编号
        $request->addParam("batchNo",        $resPayDaifuApply->mchnt_batchid); // 出款批次
        $request->addParam("orderId",        $resPayDaifuApply->mchnt_orderid); // 出款订单号
        $request->addParam("product",        ''); // 出款产品类，为空走委托结算出款
        $request->addParam("pageNo",         1); // 分页页码
        $request->addParam("pageSize",       100); // 分页每页数目 
        $response = YopClient3::post($this->cfg->C('daifuQueryUrl'), $request);
        
        if($response->validSign==1) { // 平台内部签名验证成功
            // 取得<易宝>返回结果
            $resData = Utils::objectToArray($response);
            // 根据返回结果更新 model('PayDaifuApply')表
            if( $resData['result'] == NULL) {
                return retMsgFalse($resData['error']);
            } else {
                // return json_encode($response, true);
                return $this->yopRetHandle($resData['result'], $resPayDaifuApply);
            }
        } else { // 平台内部签名验证失败
            return paySignFalse();
        } 
    }
    
    private function yopRetHandle($resData, $resPayDaifuApply) {
        $errorCode = $resData['errorCode'];
        $listData  = $resData['list'][0];
        $statusCode = $listData['transferStatusCode'];
        if($errorCode == 'BAC001') {
            if((array_key_exists('bankMsg', $listData))) {
                $bankMsg = $listData['bankMsg'];
            } else {
                $bankMsg = null;
            }
            $queryDataUpdate['status']      = $this->statusCode($statusCode);
            $queryDataUpdate['bank_status'] = $listData['bankTrxStatusCode'];
            $queryDataUpdate['remark']      = $bankMsg;
            $resUpdate = model('PayDaifuApply')->updateById($queryDataUpdate, $resPayDaifuApply->id);
            if($resUpdate) {
                // 畅远向支付平台发起代付查询请求的返回
                return adminDaifuQueryTrue($resPayDaifuApply);
            } else {
                return retMsgFalse('出款查询失败');
            }
        } else {
             return retMsgFalse($listData['bankMsg']);
        }
    }

}